<?php
/**
 * Created by PhpStorm.
 * User: linyh
 * Date: 2017/11/25 0025 10:13
 */
?>
<link href="/ueditor/themes/default/css/umeditor.css" type="text/css" rel="stylesheet" />

<section class="content-header">
    <h1>
        贷款申请
    </h1>
</section>

<!-- Main content -->
<section class="content">
<div class="row">
    <div class="col-md-12">
        <!-- general form elements -->
        <div class="box box-default">
            <!-- /.box-header -->
            <!-- form start -->
            <form id="form" role="form" class="form-horizontal">
                <div class="box-body">
                    <div class="form-group">
                        <label for="input1"  class="col-sm-3 control-label">申请金额</label>
                        <div class="col-sm-6">
                        <input id="input1" class="form-control" type="number" min="1" name="num" value="" placeholder="请输入贷款金额">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="input2"  class="col-sm-3 control-label">申请对象</label>
                        <div class="col-sm-6">
                        <input id="input2" class="form-control" type="text" disabled>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="input3"  class="col-sm-3 control-label">贷款周期</label>
                        <div class="col-sm-6">
                        <select id="input3" class="form-control" name="cycle">
                            <option value="" disabled selected>请选择贷款周期</option>
                        </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="input4"  class="col-sm-3 control-label">贷款利率</label>
                        <div class="col-sm-6">
                        <input id="input4" class="form-control" type="text" value="" disabled>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="input5"  class="col-sm-3 control-label">到期利息</label>
                        <div class="col-sm-6">
                        <input id="input5" class="form-control" type="text" value="" disabled>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="input6"  class="col-sm-3 control-label">资金用途</label>
                        <div class="col-sm-6">
                        <textarea name="apply" id="input6" class="form-control" rows="10" placeholder="请输入资金用途（不超过1000字）"></textarea>
                        </div>
                    </div>
                </div>
                <!-- /.box-body -->

                <div class="box-footer text-center">
                    <button type="submit" class="btn btn-success">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>
</section>

<script>
$(function() {
    $('#input2').val(global.user.country.name);

    var $select = $('[name=cycle]');
    var loanRates = global.user.country.loanRates;
    console.log(loanRates)
    console.log(global.count)
    for (var i in global.user.country.loanRates) {
        if(loanRates[i].cycle <= global.count + 1) {
            $select.append($('<option>').attr('value', loanRates[i].cycle).attr('lk-rate', loanRates[i].loan_rate).attr('lk-cycle', loanRates[i].cycle).text(loanRates[i].cycle + '个财年'));
        }
    }

    $select.change(function() {
        $option = $select.find('option:selected');
        $('#input4').val($option.attr('lk-rate'));
        $('#input5').val(Math.round($option.attr('lk-rate') * $option.attr('lk-cycle') * $('#input1').val(), 2));
    })

    $("#input1").bind('input propertychange',function(){
        $('#input5').val(Math.round($option.attr('lk-rate') * $option.attr('lk-cycle') * $('#input1').val(), 2));
    });
})


$('#form').submit(function(e) {
    e.preventDefault();
    var str = $('#form').serialize();
    console.log(str);

    $.ajax('/api/loan/loan', {
        type: 'POST',
        data: str,
        dataType: 'json',
        headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
    }).then(function(ret) {
        if(ret.state ==0) {
            alert('已提交贷款申请');
            location.href = '/loan/index';
        } else {
            alert(ret.message);
        }
    });
});

</script>